package com.sky.controller.user;

import com.sky.dataTransmit.OrdersPaymentDTO;
import com.sky.dataTransmit.OrdersSubmitDTO;
import com.sky.pageQuery.HistoryOrdersPageQuery;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.view.OrderPaymentVO;
import com.sky.view.OrderSubmitVO;
import com.sky.view.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("userOrdersController")
@RequestMapping("/user/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable Long id) throws Exception {
        log.info("客户取消订单");
        ordersService.userCancel(id);
        return Result.success("客户取消订单",null);
    }

    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单 {}",ordersSubmitDTO);
        OrderSubmitVO submitVO = ordersService.submit(ordersSubmitDTO);
        return Result.success("用户下单",submitVO);
    }

    @PutMapping("/payment")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("用户支付订单：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
//        ordersService.paySuccess(ordersPaymentDTO.getOrderNumber());
        return Result.success(/*orderPaymentVO*/);
    }

    @GetMapping("/historyOrders")
    public Result<PageResult> historyOrders(@ModelAttribute HistoryOrdersPageQuery historyOrdersPageQuery){
        log.info("用户查询历史订单 {}",historyOrdersPageQuery);
        PageResult pageResult = ordersService.historyOrders(historyOrdersPageQuery);
        return Result.success("用户查询历史订单",pageResult);
    }

    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id){
        log.info("客户催单");
        ordersService.reminder(id);
        return Result.success("客户催单",null);
    }

    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable Long id){
        log.info("再来一单");
        ordersService.repetition(id);
        return Result.success("再来一单",null);
    }

    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> orderDetail(@PathVariable Long id){
        log.info("用户查询订单详情");
        OrderVO orderVO = ordersService.orderDetail(id);
        return Result.success("用户查询订单详情",orderVO);
    }
}
